package org.yyds;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.HashSet;
import java.util.Set;

public class RedisPollingBackupWithLettuce2 {

    private static final int POLLING_INTERVAL_MS = 100; // 轮询间隔时间，单位：毫秒
    private static final String[] KEY_PATTERNS = {"*"};

    public static void main(String[] args) {
        //  redis://[password@]host[:port][/databaseNumber]
        //RedisClient redisClient = RedisClient.create("redis://password@localhost:6379/0");

        RedisClient redisClientMain = RedisClient.create("redis://localhost:6379");
        RedisClient redisClientBackup = RedisClient.create("redis://localhost:6379");
        StatefulRedisConnection<String, String> connectionMain = redisClientMain.connect();
        StatefulRedisConnection<String, String> connectionBackup = redisClientBackup.connect();
        RedisCommands<String, String> syncCommandsMain = connectionMain.sync();
        RedisCommands<String, String> syncCommandsBackup = connectionBackup.sync();

        syncCommandsMain.select(0);
        syncCommandsBackup.select(1);

        // 清空备份 Redis 中的数据
        syncCommandsBackup.flushdb();

        while (true) {
            try {
                Thread.sleep(POLLING_INTERVAL_MS); // 每隔100毫秒检查一次
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            // 获取主 Redis 中的所有键
            Set<String> mainKeys = new HashSet<>(128);
            for (String pattern : KEY_PATTERNS) {
                mainKeys.addAll(syncCommandsMain.keys(pattern));
            }

            // 获取备份 Redis 中的所有键
            Set<String> backupKeys = new HashSet<>(syncCommandsBackup.keys("*"));

            // 备份新增的键到备份 Redis
            for (String key : mainKeys) {
                if (!backupKeys.contains(key)) {
                    String dataType = syncCommandsMain.type(key);
                    if (!"string".equals(dataType)) continue;
                    String value = syncCommandsMain.get(key);
                    syncCommandsBackup.set(key, value);
                    System.out.println("Backed up key: " + key);
                }
            }

            // 从备份 Redis 恢复被删除的键
            for (String key : backupKeys) {
                if (!mainKeys.contains(key)) {
                    String dataType = syncCommandsBackup.type(key);
                    if (!"string".equals(dataType)) continue;
                    String value = syncCommandsBackup.get(key);
                    syncCommandsMain.set(key, value);
                    System.out.println("Restored key: " + key);
                }
            }

            // 更新主 Redis 中的键值对到备用 Redis
            for (String key : mainKeys) {
                if (backupKeys.contains(key)) {
                    String dataTypeMain = syncCommandsMain.type(key);
                    //String dataTypeBackup = syncCommandsBackup.type(key);
                    if (!"string".equals(dataTypeMain)) continue;
                    String valueMain = syncCommandsMain.get(key);
                    String valueBackup = syncCommandsBackup.get(key);
                    if (valueMain != null && !valueMain.equals(valueBackup)) {
                        syncCommandsBackup.set(key, valueMain);
                        System.out.println("Updated key: " + key);
                    }
                }
            }
        }
        // 关闭连接
//        connectionMain.close();
//        connectionBackup.close();
//        redisClientMain.shutdown();
//        redisClientBackup.shutdown();
    }
}
